package com.xbongbong.paas.service.impl;

import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.domain.entity.PaasCoUserEntity;
import com.xbongbong.paas.help.BusinessUserTeamHelp;
import com.xbongbong.paas.help.FormDataFormExplainHelper;
import com.xbongbong.paas.model.PaasCoUserModel;
import com.xbongbong.paas.pojo.dto.CoUserAddDTO;
import com.xbongbong.paas.pojo.dto.CoUserDeleteDTO;
import com.xbongbong.paas.pojo.dto.CoUserListDTO;
import com.xbongbong.paas.pojo.vo.CoUserAddVO;
import com.xbongbong.paas.pojo.vo.CoUserDeleteVO;
import com.xbongbong.paas.pojo.vo.CoUserListVO;
import com.xbongbong.paas.service.CoUserService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.sys.domain.entity.UserEntity;
import com.xbongbong.sys.model.UserModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author: wufeng
 * @date: 2018/9/6 17:29
 * @desrcption:
 */
@Service("coUserService")
public class CoUserServiceImpl implements CoUserService {

    private static final Logger LOG = LoggerFactory.getLogger(CoUserServiceImpl.class);

    @Resource
    private PaasCoUserModel paasCoUserModel;
    @Resource
    private UserModel userModel;
    @Resource
    private FormDataFormExplainHelper formDataFormExplainHelper;
    @Resource
    private BusinessUserTeamHelp businessUserTeamHelp;

    @Override
    public CoUserListVO list(CoUserListDTO coUserListDTO) throws XbbException {
        CoUserListVO coUserListVO = new CoUserListVO();
        try {
            Map<String, Object> map = BeanUtil.convertBean2Map(coUserListDTO);
            List<PaasCoUserEntity> list = paasCoUserModel.list(map);
            if(list == null) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            List<UserEntity> userList = userModel.findEntitys(map);
            if(userList == null) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.USER_NOT_EXIST);
            }
            List<UserEntity> coUserList = new ArrayList<>();
            for (UserEntity user: userList) {
                for (PaasCoUserEntity coUser: list) {
                    if(coUser.getCoUserId().equals(user.getUserId())){
                        coUserList.add(user);
                    }
                }
            }
            coUserListVO.setCoUserList(coUserList);
        }  catch (XbbException e) {
            throw e;
        }  catch (Exception e) {
            LOG.error("coUser.list出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return coUserListVO;
    }

    @Override
    public Map<Long, List<String>> coUserMap(CoUserListDTO coUserListDTO) throws XbbException {
        return formDataFormExplainHelper.coUserMap(coUserListDTO);
    }
    
    @Override
    public Map<Long, List<String>> coUserMapShow(Set<String> userIdSet, Collection<Long> formDataIdIn, String corpid) throws XbbException {
        return businessUserTeamHelp.coUserMapShow(userIdSet, formDataIdIn, corpid);
    }

    @Override
    public CoUserAddVO add(CoUserAddDTO coUserAddDTO) throws XbbException {
        CoUserAddVO coUserAddVO = new CoUserAddVO();
        try {
            List<String> coUserList = coUserAddDTO.getCoUserList();
            List<PaasCoUserEntity> list = new ArrayList<>();
            for (String s: coUserList) {
                PaasCoUserEntity paasCoUserEntity = new PaasCoUserEntity();
                long now = DateUtil.getInt();
                BeanUtil.copyProperties(coUserAddDTO, paasCoUserEntity);
                paasCoUserEntity.setCorpid(s);
                paasCoUserEntity.setAddTime(now);
                paasCoUserEntity.setUpdateTime(now);
                list.add(paasCoUserEntity);
            }
            paasCoUserModel.insertBatch(list);
        } catch (Exception e) {
            LOG.error("coUser数据库增加协同人出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return coUserAddVO;
    }

    @Override
    public CoUserDeleteVO delete(CoUserDeleteDTO coUserDeleteDTO) throws XbbException {
        CoUserDeleteVO coUserDeleteVO = new CoUserDeleteVO();
        try {
          PaasCoUserEntity paasCoUserEntity =  paasCoUserModel.getByKey(coUserDeleteDTO.getCoUserId().intValue(), coUserDeleteDTO.getCorpid());
          if(paasCoUserEntity == null){
              throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.COUSER_NOT_EXIST);
          }
          paasCoUserModel.delete(coUserDeleteDTO.getCoUserId().intValue(), coUserDeleteDTO.getCorpid());

          coUserDeleteVO.setCoUserId(coUserDeleteDTO.getCoUserId());
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("coUser数据库增加协同人出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return coUserDeleteVO;
    }
}
